import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import path, { dirname } from "node:path";
import viteCompression from "vite-plugin-compression";
import { viteMockServe } from "vite-plugin-mock";
import EnhanceLog from "vite-plugin-enhance-log";

const proxyUrl = "http://192.168.0.87:60063"; //测试环境

import { fileURLToPath } from "url";

const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);

export default defineConfig({
    build: {
        assetsDir: "static",
        target: ["chrome78", "chrome52", "chrome49", "chrome65"],
        chunkSizeWarningLimit: 1000,
        minify: "terser",
        cssCodeSplit: true,
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true, //打包时删除 debugger
            },
            output: {
                comments: true, // 去掉注释内容
            },
        },
        rollupOptions: {
            output: {
                manualChunks: {
                    vue: ["vue", "vue-router"],
                    echarts: ["echarts"],
                },
            },
        },
        brotliSize: false,
    },
    base: "./",
    css: {
        preprocessorOptions: {
            less: {
                javascriptEnabled: true,
            },
        },
    },
    resolve: {
        alias: {
            // 导入文件夹别名
            "@": path.resolve(_dirname, "./src"),
            vue: "vue/dist/vue.esm-bundler.js",
        },
    },
    plugins: [
        vue(),
        //console.log插件
        EnhanceLog({
            splitBy: ";",
        }),
        vueJsx({}),
        viteCompression(), //gzip压缩
        viteMockServe({
            mockPath: "mock",
            supportTs: false, // 打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件。
            watchFiles: true, // 监视文件更改
        }),
    ],
    server: {
        port: 3031,
        host: "0.0.0.0",
        open: true,
        hmr: {
            overlay: true,
        },
        proxy: {
            "/ups": {
                target: proxyUrl,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
            "/smw": {
                target: proxyUrl,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
            "/zp": {
                target: "http://10.1.0.51:50038",
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
            "/api": {
                target: "http://192.168.131.67:81",
                changeOrigin: true,
            },
        },
    },
});
